/*
 * ether.h
 *
 *  Created on: 2012-10-30
 *      Author: maciek
 */

#ifndef ETHER_H_
#define ETHER_H_
using namespace std;

#include <iostream>
#include <queue>
#include <list>
#include <fstream>
#include <cstdlib>
#include <QObject>
#include <unistd.h>
#include "worldMap.h"
#include "robot.h"
#include "signalR.h"
#include "signalToAddRobot.h"


class ether:public QObject {
  Q_OBJECT
public:
	// TODO: Paweł
	// add interfaces for Krzysiek for everything what he wants.
	//ether(); // should load log file, map, robots etc.
	ether( const double StepTime=0.1, const double SignalTTL=1, const double MaxTime=45 );

	// TODO: Paweł
	virtual ~ether();
	// TODO: Marian
	// should check if we have not got collision
	// adding new robot to system adds new signalToAddRobot to _signalsPQ.
	// _timeOfLastAction could be time when robot should be add
	void 	addRobot( robot& NewRobot );
	// TODO: Paweł
	void 	addSignal( signalR& NewSignal );
	// TODO: Paweł
	void 	executeSignal( signalR& Signal ); // write something to log
	// Marian believes it is done
	int 	loadMap( fstream& FileDes );
	// Marian believes his part is done
	int 	loadRobots( fstream& FileDes );
	// TODO: Paweł/Dziergwa
	int              runSimulation( const double MaxTime );
	int		runSimulation( void );
	// TODO: Paweł
	void 	setMaxTime( const double Time );
	// TODO: Paweł
	void 	setStepTime( const double StepTime );
	// TODO: Paweł
	void 	setRSignalTTL( const double TTL );

	// Marian believes it is done
	void	showMap();
	int**	returnMap(); // pierdoly Dziergwy
	complex<int> returnMapSize(); // pierdoly Dziergwy
	list< robot* > returnRobots();
	double returnStepTime();
	double timereturn();
	void 	setlogOn();
	void 	setlogOff();
	  void setTime(double _time);
	int		loadLog( fstream& FileDes );
	void resetPriority();
signals:
  void timeChanged(double qtime);
private:
	// configuration parameters:
	int							_logOn; // flag for i/o log file
	double						_stepTime;	// of simulation
	double						_signalTTL; // time to live of signal
	double 						_maxTime;	// and then we turn off lights
	double						_signalRange; // how far signal can go
	// our world
	double 						_currentTime;
	worldMap 					_map;
	priority_queue<signalR*, vector<signalR*>, signalRTimeCompare> 	_signalsPQ;
	list< robot* > 				_robots;
	fstream* 					_log;
	int 	checkCollisions( void );
	/* TODO: Dziergwa/Paweł: */
	int 	moveRobotsToPositions( const double& InTime );
	// Marian believes it is done
	int		sendToLog( string LogMsg );

	int		collectRobotsSignals(void);

	int 	signalDelivery(signalR &signal);

/*
	ether(){
		_logOn = 0;
	}
*/
};

#endif /* ETHER_H_ */
